<script setup lang="ts">
import HyForm from '@/base-ui/form';
import { useStore } from '@/store';
import { ref, watch } from 'vue';

const props = defineProps({
  modalConfig: {
    type: Object,
    default: () => ({})
  },
  otherInfo: {
    type: Object,
    default: () => ({})
  },
  defaultInfo: {
    type: Object,
    default: () => ({})
  },
  pageName: {
    type: String,
    require: true
  }
});
const dialogVisible = ref(false);
defineExpose({
  dialogVisible
});

const formData = ref<any>({});
watch(
  () => props.defaultInfo,
  (newValue) => {
    for (const item of props.modalConfig.formItems) {
      formData.value[`${item.field}`] = newValue[`${item.field}`];
    }
  }
);

// 点击确定按钮的逻辑
const store = useStore();
const handleConfirmClick = () => {
  dialogVisible.value = false;
  if (Object.keys(props.defaultInfo).length) {
    // 编辑
    console.log('编辑用户');
    store.dispatch('systemModule/editPageDataAction', {
      pageName: props.pageName,
      editData: { ...formData.value, ...props.otherInfo },
      id: props.defaultInfo.id
    });
  } else {
    // 新建
    console.log('新建用户');
    store.dispatch('systemModule/createPageDataAction', {
      pageName: props.pageName,
      newData: { ...formData.value, ...props.otherInfo }
    });
  }
};
</script>
<template>
  <div class="page-modal">
    <el-dialog title="新建用户" v-model="dialogVisible" width="30%" center destroy-on-close>
      <hy-form v-bind="modalConfig" v-model="formData"></hy-form>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirmClick"> 确 定 </el-button>
        </span>
      </template></el-dialog
    >
  </div>
</template>
<style lang="less" scoped></style>
